(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("deliveryServiceService", deliveryServiceService);
	deliveryServiceService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'modalService', 'gridService', '$filter' ];
	function deliveryServiceService($q, loggerFactory, commonService, ajaxServiceFactory, modalService, gridService, $filter) {
		var log = loggerFactory.getLogger("deliveryServiceService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			dropDownDataOptions : {
				url : 'do/cda/deliveryService/dropDownDataOptions',
				method : 'get'
			}
		});
		function dropDownDataOptions() {
			var defer = $q.defer();
			ajaxService.dropDownDataOptions().then(function(response) {
				var options = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						options[key] = value;
					});
				}
				defer.resolve(options);
			});
			return defer.promise;
		}
		function initDropDownDataOptions(ctrl) {
			var defer = $q.defer();
			dropDownDataOptions().then(function(response) {
				ctrl.yesOrNoOptions = response.yesOrNoOptions;
				ctrl.siteCodeOptions = response.siteCodeOptions;
				ctrl.serviceCodeOptions = response.serviceCodeOptions;
				ctrl.deliveryTypeOptions = response.deliveryTypeOptions;
				ctrl.deliSvcStatusOptions = response.deliveryServiceStatusOptions;
				defer.resolve();
			}, angular.noop);
			return defer.promise;
		}
		function initGridTable($scope, ctrl, listFlag) {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.deliveryServiceInfo.tableConfigs.table, seleRowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.deliveryService.deliSvcInfoProps';//dynamicParams
			ctrl.innerGridOptions.verifyData = 'vm.deliveryServiceOld.deliSvcInfoProps';
			function seleRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedCustPropSeleRows = selectedRows;
			}
			ctrl.deliveryTypeChanged = function(entity, formCtrl, isInitialized) {
				
				ctrl.deliveryService.deliSvcInfoProps = [];
				var gridConfigs = ctrl.layoutConfig.formConfigs.general.gridConfigs;
				if (gridConfigs) {
					var deliSvcInfoProps = [];
					angular.forEach(gridConfigs, function(gridConfig, key) {
						if (gridConfig.dynamicParameter && gridConfig.dynamicParameter.category == 'DELIVERYSERVICE' && ctrl.deliveryService.deliveryType == gridConfig.dynamicParameter.subCategory) {
							var gridConfig = angular.copy(gridConfig);
							deliSvcInfoProps.push(gridConfig);
						}
					});
					if (!ctrl.deliveryService.dynamicParams) {								
						ctrl.deliveryService.dynamicParams = {};
					}
					//if update page begin
					ctrl.deliveryService.oldDynamicParams = angular.copy(ctrl.deliveryService.dynamicParams);
					//if update page end code.
					angular.forEach(deliSvcInfoProps, function(deliSvcInfoProp, index) {
						ctrl.deliveryService.dynamicParams[deliSvcInfoProp.name] = deliSvcInfoProp;
						
						angular.merge(deliSvcInfoProp, deliSvcInfoProp.dynamicParameter);
						// change 
						var corProf = angular.copy(deliSvcInfoProp);
						corProf.dynamicParams = {};
						corProf.dynamicParams[deliSvcInfoProp.name] = deliSvcInfoProp;
						//set value for update page.
						if (corProf.dynamicParams[deliSvcInfoProp.name]
								&& corProf.dynamicParams[deliSvcInfoProp.name].dynamicParameter
								&& ctrl.deliveryService.oldDynamicParams[deliSvcInfoProp.name]
								&& $filter("encode")(corProf.dynamicParams[deliSvcInfoProp.name].dynamicParameter.id) == $filter("encode")(ctrl.deliveryService.oldDynamicParams[deliSvcInfoProp.name].id)) {
							corProf.dynamicParams[deliSvcInfoProp.name].value = ctrl.deliveryService.oldDynamicParams[deliSvcInfoProp.name].value;
						}
						ctrl.deliveryService.deliSvcInfoProps.push(corProf);
					});
				}
			}
		}
		return {
			initDropDownDataOptions : initDropDownDataOptions,
			initGridTable : initGridTable
		};
	}
})(angular);